var app = getApp();
var config = require('../../utils/config')
var host = config.host

Page({
  data: {
    // text:"这是一个页面"  
    array: ["中国", "美国", "巴西", "日本"],
    toast1Hidden: true,
    modalHidden: true,
    modalHidden2: true,
    notice_str: '',
    index: 0,
    userType: 0,
    userInfo: {},
    userStatus: 0,
    userOpenID: '',
    textContent: '',
    photo: '',
    user_photo: '',
    userLocatLnt: '',
    userLocatLat: '',
    userLocatAcc: '',
    formDataCheckOk: false,
    radioItems: [
      { name: '男', value: '99' },
      { name: '女', value: '100', checked: true },
      { name: '保密', value: '0' }
    ],
    checkboxItems: [
      { name: '意外保险', value: 'accident', checked: true },
      { name: '医疗保险', value: 'hospital' },
      { name: '少儿保险', value: 'child'},
      { name: '养老保险', value: 'endowment' },
      { name: '人寿保险', value: 'life'},
      { name: '其他', value: 'other' }
    ],
  },
  toast1Change: function (e) {
    this.setData({ toast1Hidden: true });
  },
  //弹出确认框  
  modalTap: function (e) {
    this.setData({
      modalHidden: false
    })
  },
  confirm_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '提交成功'
    });
  },
  cancel_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '取消成功'
    });
  },
  //弹出提示框  
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  onLoad: function (options) {

    wx.setNavigationBarTitle({ title: '欢迎注册' })

    console.log("注册页面加载开始")
    console.log(app.globalData.userType)
    console.log(app.globalData.userOpenID)

    this.setData({
      userType: app.globalData.userType,
      userOpenID: app.globalData.userOpenID,
      userInfo: app.globalData.userInfo,
      user_photo: app.globalData.userInfo.avatarUrl,
    })
    // 页面初始化 options为页面跳转所带来的参数  
    console.log(this.data.user_photo)
    console.log("注册页面加载结束")

    let that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        that.setData({
          userLocatLnt: res.longitude,
          userLocatLat: res.latitude,
          userLocatAcc: res.accuracy
        })
      }
    })
  },
  formCheck: function (e) {
    console.log("formCheck")
    console.log(e.detail.value)
    console.log("formCheck")
    // var subValue = e.detail.value.textarea 
    // if (subValue == null || subValue == "") { 

    // }
  },
  formSubmit: function (e) {
    var that = this
    var formData = e.detail.value
    that.formCheck(e)
    // that.data.userLocatLnt
    // that.data.userLocatLat
    // that.data.userLocatAcc
    //[校验数据完备性]
    that.setData({
      formDataCheckOk: true
    })
    console.log(that.data.formDataCheckOk)
    if (that.data.formDataCheckOk) {
      wx.request({
        url: config.config.registerUrl +
        '?openid=' + that.data.userOpenID +
        '&userType=' + that.data.userType +
        '&userPhoto=' + encodeURIComponent(that.data.user_photo) +
        '&lnt=' + that.data.userLocatLnt +
        '&lat=' + that.data.userLocatLat +
        '&acc=' + that.data.userLocatAcc,
        data: formData,
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.result) {
            that.modalTap();
            wx.redirectTo({
              url: '../local/local'
            })
          } else {
            console.log(res.data.hint)
            //Debug
            that.modalTap();
            wx.redirectTo({
              url: '../local/local'
            })

          }
        }, fail: function (e) {
          console.log(e)
        }
      })
    }
  },
  fileUpload: function (e) {
    console.log(host)
    let that = this;
    wx.uploadFile({
      url: config.config.uplodaPhotoUrl,
      filePath: that.data.photo,
      name: 'user_photo',
      header: {
        'content-type': 'application/json'
      },
      formData: {
        openid: that.data.userOpenID,
      },
      success: function (res) {
        console.log("add photo success", res);
        var json_data = JSON.parse(res.data)
        console.log(json_data)
        that.setData({
          user_photo: config.config.urlBase + json_data.image_url
        })
      },
      fail: function (res) {
        console.log("add photo fail", res);
      },
    })
  },
  formReset: function () {
    console.log('form发生了reset事件');
    this.modalTap2();
  },
  chooseImageTap: function () {
    let that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        that.setData({
          photo: res.tempFilePaths[0],
        })
        console.log(that.data.photo);
        that.fileUpload()
      }
    })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems
    });
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);

    var checkboxItems = this.data.checkboxItems, values = e.detail.value;
    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      checkboxItems[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkboxItems[i].value == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    this.setData({
      checkboxItems: checkboxItems
    });
  },
})  